//
// Created by 86150 on 2021/10/17.
//

#ifndef BJUT_CPP3_CLIENT_H
#define BJUT_CPP3_CLIENT_H
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QFrame>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QRadioButton>
#include <QtWidgets/QTextBrowser>
#include <QtWidgets/QWidget>
#include "CComplex.h"
#include "containers.h"
#include "ui_calculator.h"
#include<winsock2.h>
#pragma comment(lib, "ws2_32.lib")

class client: public QWidget
{
	private:
		Ui::Form my_ui;
		WSADATA my_wsa;
		SOCKET clnt_sock;
		SOCKADDR_IN serv_addr;
		std::string ip;
		std::string port;

		void Compute_Complex()
		{
			CComplex a = CComplex(
						std::stoi(my_ui.lineEdit->text().toStdString()),
						std::stoi(my_ui.lineEdit_2->text().toStdString())
					);
			CComplex b = CComplex(
						std::stoi(my_ui.lineEdit_3->text().toStdString()),
						std::stoi(my_ui.lineEdit_4->text().toStdString())
					);
			std::string temp("");

			if (my_ui.radioButton->isChecked()) {
				temp<<(a + b);
				my_ui.textBrowser->setText(QString::fromStdString(temp));
				return;
			}
			if (my_ui.radioButton_2->isChecked()) {
				temp<<(a - b);
				my_ui.textBrowser->setText(QString::fromStdString(temp));
				return;
			}
			if (my_ui.radioButton_3->isChecked()) {
				temp<<(a * b);
				my_ui.textBrowser->setText(QString::fromStdString(temp));
				return;
			}
			if (my_ui.radioButton_4->isChecked()) {
				temp<<(a / b);
				my_ui.textBrowser->setText(QString::fromStdString(temp));
				return;
			}
		}
		void Compute_3D()
		{
			std::string temp("");
			if (my_ui.radioButton_5->isChecked()) {
				temp<<cube(stoi(my_ui.lineEdit_5->text().toStdString()));
				my_ui.textBrowser_2->setText(QString::fromStdString(temp));
				return;
			}
			if (my_ui.radioButton_6->isChecked()) {
				temp<<sphere(stoi(my_ui.lineEdit_5->text().toStdString()));
				my_ui.textBrowser_2->setText(QString::fromStdString(temp));
				return;
			}
			if (my_ui.radioButton_7->isChecked()) {
				temp<<cylinder(stoi(my_ui.lineEdit_5->text().toStdString()));
				my_ui.textBrowser_2->setText(QString::fromStdString(temp));
				return;
			}
		}
		void convey(void* send, void* recv)
		{
			int len1;
			int len2;

			if (WSAStartup(MAKEWORD(2, 1), &my_wsa) != 0) {
				printf("error1\n");
				exit(1);
			}

			clnt_sock = socket(PF_INET, SOCK_STREAM, 0);
			if (clnt_sock == INVALID_SOCKET) {
				printf("error2\n");
				exit(1);
			}

			memset(&serv_addr, 0, sizeof(serv_addr));
			serv_addr.sin_family = AF_INET;
			serv_addr.sin_addr.s_addr = inet_addr(ip.c_str());
			serv_addr.sin_port = htons(atoi(port.c_str()));

			if (::connect(clnt_sock, (SOCKADDR*)&serv_addr, sizeof(serv_addr)) == SOCKET_ERROR) {
				printf("error3\n");
				exit(1);
			}

			len1 = ::send(clnt_sock, (char*)send, sizeof(send) - 1, 0);
			if (len1 == -1) {
				printf("error5\n");
				exit(1);
			}

			len2 = ::recv(clnt_sock, (char*)recv, sizeof(recv) - 1, 0);
			if (len2 == -1) {
				printf("error4\n");
				exit(1);
			}

			(void*)send;
			(void*)recv;
			closesocket(clnt_sock);
			WSACleanup();
		}
		void Compute_3D2()
		{
			std::string temp("");
			if (my_ui.radioButton_5->isChecked()) {
				int i = 1;
				cube my_cube(stoi(my_ui.lineEdit_5->text().toStdString()));
				convey((void*)&i, (void*)&i);
				convey((void*)&my_cube, (void*)&temp);
				my_ui.textBrowser_2->setText(QString::fromStdString(temp));
				return;
			}
			if (my_ui.radioButton_6->isChecked()) {
				int i = 2;
				sphere my_sphere(stoi(my_ui.lineEdit_5->text().toStdString()));
				convey((void*)&i, (void*)&i);
				convey((void*)&my_sphere, (void*)&temp);
				my_ui.textBrowser_2->setText(QString::fromStdString(temp));
				return;
			}
			if (my_ui.radioButton_7->isChecked()) {
				int i = 3;
				cylinder my_cylinder(stoi(my_ui.lineEdit_5->text().toStdString()));
				convey((void*)&i, (void*)&i);
				convey((void*)&my_cylinder, (void*)&temp);
				my_ui.textBrowser_2->setText(QString::fromStdString(temp));
				return;
			}
		}

	public:
		client(std::string IP = "", std::string Port = ""): ip(IP), port(Port)
		{
			my_ui.setupUi(&my_ui);
			QObject::connect(my_ui.pushButton, &QPushButton::clicked, this, &client::Compute_Complex);
			QObject::connect(my_ui.pushButton_2, &QPushButton::clicked, this, &client::Compute_3D2);
			QMetaObject::connectSlotsByName(this);
			my_ui.show();
		}



};

#endif //BJUT_CPP3_CLIENT_H
